#include "SimpleElementWithTable.h"
#include "MultiplyTable.h"

#include <stdexcept>
#include <vector>

using std::invalid_argument;
using std::vector;

static Word32** g_MultiplyTable[31] = {
	0,(Word32**)Muliply2Table,0,0,0,0,(Word32**)Multiply7Table
};
//static bool initialed[];

SimpleElementWithTable::SimpleElementWithTable(Word32 data, size_t dimen, Word32 prime)
:SimpleElement(data, dimen, prime)
{
	if(dimen > NMaxValue)
	{
		throw invalid_argument("The dimen n is too large");
	}
}

SimpleElementWithTable SimpleElementWithTable::operator +(const SimpleElementWithTable &rightValue) const
{
	SimpleElementWithTable resultElement(0, m_Dimen, m_Prime);
	resultElement.setData(m_Data ^ rightValue.m_Data);
	return resultElement;
}

SimpleElementWithTable SimpleElementWithTable::operator -(const SimpleElementWithTable& rightValue) const
{
	return operator+(rightValue);
}

SimpleElementWithTable SimpleElementWithTable::operator *(const SimpleElementWithTable &rightValue) const
{
	SimpleElementWithTable resultElement(0, m_Dimen, m_Prime);
	resultElement.setData( g_MultiplyTable[m_Dimen][m_Data][rightValue.getData()] );
	return resultElement;
}

SimpleElementWithTable& SimpleElementWithTable::operator +=(const SimpleElementWithTable &rightValue)
{
	m_Data ^= rightValue.m_Data;
	return *this;
}

SimpleElementWithTable& SimpleElementWithTable::operator -=(const SimpleElementWithTable &rightValue)
{
	return operator+=(rightValue);
}

SimpleElementWithTable& SimpleElementWithTable::operator *=(const SimpleElementWithTable &rightValue)
{
	m_Data = g_MultiplyTable[m_Dimen][m_Data][rightValue.getData()];
	return *this;
}